package com.sf.fy.uav.common.utils;


import java.io.Serializable;

/**
 * 响应体
 *
 * @param <T>
 * @Author LTQ
 */
public class Rs<T> implements Serializable {
    private static final long serialVersionUID = -1845782287674831578L;

    private Integer code;

    private String msg;

    private T data;

    public boolean isSuccess() {
        return ResultCode.SUCCESS.getCode().equals(this.code);
    }

    public boolean isFail() {
        return !isSuccess();
    }

    public static <T> Rs<T> auto(boolean flag) {
        return auto(flag, "");
    }

    public static <T> Rs<T> auto(T data, boolean flag) {
        if (flag) {
            return success(data);
        }
        return failed(data);
    }

    public static <T> Rs<T> auto(boolean flag, String msg) {
        if (flag) {
            return Rs.success();
        } else {
            return Rs.failed(msg);
        }
    }

    public static <T> Rs<T> success() {
        return restResult(null, ResultCode.SUCCESS, null);
    }

    public static <T> Rs<T> success(T data) {
        return restResult(data, ResultCode.SUCCESS, null);
    }

    public static <T> Rs<T> success(T data, String msg) {
        return restResult(data, ResultCode.SUCCESS, msg);
    }

    public static <T> Rs<T> failed() {
        return restResult(null, ResultCode.FAIL, null);
    }

    public static <T> Rs<T> failed(String msg) {
        return restResult(null, ResultCode.FAIL, msg);
    }

    public static <T> Rs<T> failed(ResultCode resultCode, String msg) {
        return restResult(null, resultCode, msg);
    }

    public static <T> Rs<T> failed(T data) {
        return restResult(data, ResultCode.FAIL, null);
    }

    public static <T> Rs<T> failed(T data, String msg) {
        return restResult(data, ResultCode.FAIL, msg);
    }

    public static <T> Rs<T> error() {
        return restResult(null, ResultCode.ERROR, null);
    }

    public static <T> Rs<T> error(T data) {
        return restResult(data, ResultCode.ERROR, null);
    }

    public static <T> Rs<T> error(T data, String msg) {
        return restResult(data, ResultCode.ERROR, msg);
    }

    private static <T> Rs<T> restResult(T data, ResultCode resultCode, String msg) {
        Rs<T> apiResult = new Rs<>();
        apiResult.setCode(resultCode.getCode());
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public Integer getCode() {
        return code;
    }

    public Rs<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Rs<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Rs<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "Rs{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
